package com.nioSocketCore.chapter01;

import java.nio.ByteBuffer;

/**
 * @author : wushikai
 * <p>
 * date : 2022-04-29
 */
public class Test08 {


    /**
     * 方 法 int remaining()的作用：返回  "当前位置"  与 limit 之间的元素数 。
     */
    public static void main(String[] args) {

        byte[] bytes = {1, 2, 3};

//        ByteBuffer buffer = ByteBuffer.wrap(bytes);
        ByteBuffer buffer = ByteBuffer.allocate(10);
        buffer.limit(3); //只能放3个
        System.out.println(" A capacity () =" + buffer.capacity() + " limit() =" + buffer.limit() + "  position() = " + buffer.position());
        buffer.put((byte) 'a');
        buffer.put((byte) 'b');
        buffer.put((byte) 'c');
        //buffer.put((byte) 'd');
        System.out.println(buffer.get(0));
        System.out.println(buffer.get(1));
        System.out.println(buffer.get(2));
        System.out.println(" B capacity () =" + buffer.capacity() + " limit() =" + buffer.limit() + "  position() = " + buffer.position());
        buffer.flip();  //本来你不可以再放入 data ,  flip 之后 , 将会覆盖原data
        buffer.put((byte) 'd');
        System.out.println(buffer.get(0));
        System.out.println(buffer.get(1));
        System.out.println(buffer.get(2));

        System.out.println(" C capacity () =" + buffer.capacity() + " limit() =" + buffer.limit() + "  position() = " + buffer.position());

    }


}
